package com.aynu.gulimall.cart.web;

import com.aynu.common.constant.AuthServerContant;
import com.aynu.gulimall.cart.interceptor.CartInterceptor;
import com.aynu.gulimall.cart.serivce.CartService;
import com.aynu.gulimall.cart.to.UserInfoTo;
import com.aynu.gulimall.cart.vo.CartItemVo;
import com.aynu.gulimall.cart.vo.CartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author mj
 * @version 1.0
 * @date 2021/3/20 18:11
 */
@Controller
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * cookie：user-key：存储信息，标识用户身份，一个月过期
     * 登陆：session有
     * 没登陆：按照cookie里面带来的user-key来做
     * 第一次：如果没有临时用户，帮忙创建一个临时用户
     * @param model
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model){
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    /**
     * 添加购物车
     * RedirectAttributes:
     *      addFlashAttribute:将数据放在session里面可以在页面中取出，但是只能取一次
     *      addAttribute：将数据在url进行拼接
     * @param skuId
     * @param num
     * @param attributes
     * @return
     */
    @GetMapping("addToCart")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("num") Integer num,
                          RedirectAttributes attributes){
        cartService.addToCart(skuId,num);
        attributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * 根据skuid查询购物车中商品信息,并重定向到添加成功页面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String success(@RequestParam("skuId") Long skuId,Model model){
        CartItemVo cartItemVo = cartService.getCartBySkuId(skuId);
        model.addAttribute("item",cartItemVo);
        return "success";
    }

    /**
     * 清空购物车
     * @return
     */
    @GetMapping("/clearCart")
    public String clearCart(){
        cartService.clearCart("");
        return "/cart.html";
    }

    /**
     * 修改项目状态
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        cartService.checkItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改商品数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.countItem(skuId,num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 获取当前选中的购物车的信息
     * @return
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo> getCurrentUserCartItem(){
        return cartService.currentUserCartItems();
    }
}
